<?php include _include(APP_PATH.'view/htm/header.inc.htm');?>

<?php if($route == 'comment' && $action == 'update') {
		$form_title = lang('reply');
		$form_action = url("comment-update-$pid");
		$form_submit_txt = lang('reply');
		$form_subject = $thread['subject'];
		$form_message = $comment['message'];
		$form_doctype = $comment['doctype'];
		$quotepid = $comment['quotepid'];
		$location = $referer ? $referer : $thread['url'];
	} elseif($route == 'comment' && $action == 'create') {
		$form_title = lang('reply');
		$form_action = url("comment-create-$tid");
		$form_submit_txt = lang('reply');
		$form_subject = '';
		$form_message = '';
		$form_doctype = 1;
		$location = $referer ? $referer : $thread['url'];
		$filelist = array();
	}
	
	// hook comment_start_init.htm
	
	$filelist += (array)_SESSION('tmp_website_files');
?>

<!--{hook comment_start.htm}-->

<div class="row">
	<div class="col-lg-10 mx-auto">
		<div class="card">
			<div class="card-header">
				<?php echo $form_title; ?>
			</div>
			<div class="card-body">
				<form action="<?php echo $form_action;?>" method="post" id="form">
					<input type="hidden" name="doctype" value="<?php echo $form_doctype;?>" />
					<input type="hidden" name="safe_token" value="<?php echo $safe_token;?>" />
					<input type="hidden" name="quotepid" value="<?php echo $quotepid;?>" />
					<!--{hook comment_quotepid_before.htm}-->
					<div class="form-group">
						<textarea class="form-control" placeholder="<?php echo lang('message');?>" name="message" id="message" style="height: 300px;"><?php echo $form_message;?></textarea>
					</div>
					
					<!--{hook comment_message_after.htm}-->

					<div class="d-flex justify-content-between">
						<div class="attachlist_parent">
							<a class="small text-left" href="javascript:void(0)">
								<label class="addattach" id="addattach">
									<i class="icon-folder-open-o"></i> 
									<?php echo lang('add_attach');?>
									<input type="file"  multiple="multiple" class="invisible" />
								</label>
							</a>
							<?php echo data_file_list_html($filelist, TRUE, TRUE);?>
							<!--{hook comment_bottom_right.htm}-->
						</div>
						<div class="text-right">
							<button type="submit" class="btn btn-primary" id="submit" data-loading-text="<?php echo lang('submiting');?>..."> <?php echo $form_submit_txt;?> </button>
							<!--{hook comment_bottom_left.htm}-->
						</div>
					</div>
					<!--{hook comment_submit_after.htm}-->
				</form>
			</div>
		</div>
	</div>
</div>

<!--{hook comment_end.htm}-->

<?php include _include(APP_PATH.'view/htm/footer.inc.htm');?>
<script ajax-eval="true">
	<!--{hook comment_js_start.htm}-->

	var path = '<?php echo $conf['path'];?>';
	var safe_token = '<?php echo $safe_token;?>';
	var body = $('body');
	var jform = $('#form');
	body.on('submit', '#form', function () {
		var jthis = $(this);
		var jsubmit = jthis.find('#submit');
		jsubmit.button('loading');
		jthis.reset();
		var postdata = jthis.serialize();
		$.xpost(jthis.attr('action'), postdata, function (code, message) {
			if (code == 0) {
				$.alert(message);
				jsubmit.button(message).delay(1000).location('<?php echo $location;?>');
			} else if (xn.is_number(code)) {
				$.alert(message);
				jsubmit.button('reset');
			} else {
				$.alert(message);
				jsubmit.button('reset');
			}
		});
		return false;
	});

	var jattachparent = $('.attachlist_parent');
	$('#addattach').on('change', function (e) {
		var files = xn.get_files_from_event(e);
		if (!files) return;

		/*并发下会 服务端 session 写入会有问题，由客户端控制改为串行*/
		if (!jattachparent.find('.attachlist').length) {
			jattachparent.append('<fieldset class="fieldset m-0 p-0"><legend><?php echo lang('uploaded_attach');?></legend><ul class="list-unstyled attachlist nowrap"><ul></fieldset>');
		}

		var jprogress = jattachparent.find('.progress');
		if (!jprogress.length) {
			jprogress = $('<div class="progress"><div class="progress-bar" role="progressbar" style="width: 25%;" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100">25%</div></div>').appendTo(jattachparent);
		}
		jprogressbar = jprogress.find('.progress-bar');

		$.each_sync(files, function (i, callback) {
			var file = files[i];
			xn.upload_file(file, xn.url('attach-create', {safe_token: safe_token}), {is_image: 0}, function (code, message) {
				if (code != 0) return $.alert(message);
				/*把文件 append 到附件列表*/
				var url = message.url;
				var filetype = message.filetype;
				var aid = message.aid;
				var s = '<a href="' + url + '" target="_blank" class="d-block ellipsis"><i class="icon filetype ' + filetype + '"></i> ' + message.orgfilename + '</a>';

				s += '<span class="btn px-1 py-0 delete"><i class="icon-remove"></i></span>';

				var attachlist = '<li aid="' + aid + '" class="d-flex justify-content-between p-1">' + s + '</li>';
				$('.attachlist').append(attachlist);
				callback();
				jprogress.hide();
			}, function (percent) {
				percent = xn.intval(percent);
				jprogressbar.css('width', percent + '%');
				jprogressbar.text(percent + '%');
				jprogress.show();
				console.log('progress:' + percent);
			});
		});
	});

	/*删除附件*/
	jattachparent.on('click', '.delete', function () {
		var jlink = $(this);
		var jli = jlink.parents('li');
		var aid = jli.attr('aid');
		if (!window.confirm(lang.confirm_delete)) return false;
		$.xpost(xn.url('attach-delete-' + aid), function (code, message) {
			if (code != 0) return $.alert(message);
			jlink.parent().remove();
		});
		return false;
	});

	<!--{hook comment_js_end.htm}-->

	$('li[data-active="fid-<?php echo $fid;?>"]').addClass('active');
</script>
<!-- 尽量把JS写到上面的script里 -->
<!--{hook comment_js.htm}-->
